﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public class XL_DO_HOA
{
    private static Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
    {
        double multiplier = 0;
        string layout;

        if (currH > currW) layout = "portrait";
        else layout = "landscape";

        switch (layout.ToLower())
        {
            case "portrait":
                if (destH > destW)
                {
                    multiplier = (double)destW / (double)currW;
                }
                else
                {
                    multiplier = (double)destH / (double)currH;
                }
                break;
            case "landscape":
                if (destH > destW)
                {
                    multiplier = (double)destW / (double)currW;
                }

                else
                {
                    multiplier = (double)destH / (double)currH;
                }
                break;
        }

        return new Size((int)(currW * multiplier), (int)(currH * multiplier));
    }

    public static void SetImage(PictureBox pb)
    {
        try
        {
            Image img = pb.Image;
            Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height);
            Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);
            Graphics gfx = Graphics.FromImage(img);
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            pb.Image = null;
            pb.SizeMode = PictureBoxSizeMode.CenterImage;
            pb.Image = finalImg;
        }
        catch (System.Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
}
